﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateInstance.h"
#include "CommandIds.h"
#include "GbmpNew.h"
#include "CommandRegister.h"
#include "IUserTransaction.h"
#include "FamilyUtils.h"
#include "IFamily.h"
#include "IFamilyManager.h"
#include "UiDocumentViewUtils.h"
#include "IFamilyLoadOptions.h"
#include "FamilyDocumentUtility.h"
#include "IModelView.h"
#include "IDocumentManager.h"
#include "FilePath.h"
#include "IFileExtensions.h"
#include "IPolyCurve.h"
#include "IPolygon.h"
#include "ILine2d.h"
#include "IBody.h"
#include "GmBodyBuilder.h"
#include "IGraphicsBRepBody.h"
#include "IGraphicsElementShape.h"
#include "IPureGraphicsElement.h"
#include "UiCommonDialog.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace Sample;
using namespace gcmp;
using namespace gfam;

void InitFamilyDocument(IDocument* pDbDoc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDbDoc != nullptr, L"pDbDoc为空", L"GDMP", L"2023-09-30");

    // 创建形体
    OwnerPtr<IPolyCurve> opPolyCurve = IPolyCurve::Create();
    Vector2d pt1(-100, -100), pt2(100, -100), pt3(100, 100), pt4(-100, 100);
    OwnerPtr<ILine2d> opLine2d1 = ILine2d::Create(pt1, pt2);
    OwnerPtr<ILine2d> opLine2d2 = ILine2d::Create(pt2, pt3);
    OwnerPtr<ILine2d> opLine2d3 = ILine2d::Create(pt3, pt4);
    OwnerPtr<ILine2d> opLine2d4 = ILine2d::Create(pt4, pt1);
    opPolyCurve->AddBackCurve(TransferOwnership(opLine2d1));
    opPolyCurve->AddBackCurve(TransferOwnership(opLine2d2));
    opPolyCurve->AddBackCurve(TransferOwnership(opLine2d3));
    opPolyCurve->AddBackCurve(TransferOwnership(opLine2d4));
    DBG_WARN_AND_RETURN_VOID_UNLESS(opPolyCurve->IsValid(), L"多段线不合法", L"GDMP", L"2023-09-30");

    OwnerPtr<IPolygon> opPolygon = IPolygon::Create();
    opPolygon->AddPolyCurveByClone(opPolyCurve.get());
    DBG_WARN_AND_RETURN_VOID_UNLESS(opPolygon->IsRegularized(), L"多边形不合法", L"GDMP", L"2023-09-30");

    //创建扫掠路径
    std::vector<OwnerPtr<ICurve2d>> pathCurves;
    pathCurves.emplace_back(ILine2d::Create(Vector2d(0, 0), Vector2d(1000, 0)));

    //创建扫掠体
    Coordinate3d coord(Vector3d::Zero, Vector3d::UnitZ);
    OwnerPtr<IBody> opBrepBody = GmBodyBuilder::CreateSweepBody(opPolygon, pathCurves, coord);
    DBG_WARN_AND_RETURN_VOID_UNLESS(opBrepBody->IsValid(), L"体为空", L"GDMP", L"2023-09-30");

    OwnerPtr<IGraphicsBRepBody> opGraphicsBrepBody = IGraphicsBRepBody::Create(TransferOwnership(opBrepBody));
    OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(GraphicsRenderLayer::Model);
    opGraphicsElementShape->AddChild(TransferOwnership(opGraphicsBrepBody));
    IPureGraphicsElement* pPureGraphicsElement = IPureGraphicsElement::Create(pDbDoc, TransferOwnership(opGraphicsElementShape));
}

REGISTER_COMMAND(CmdCreateInstance);

CmdCreateInstance::CmdCreateInstance() : CommandBase(ID_CMD_CREATE_INSTANCE, true)
{
}

gcmp::OwnerPtr<gcmp::IAction> CmdCreateInstance::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    std::wstring strFamilyName = L"FilledRegion";
    IDocument *pDoc = FamilyDocumentUtility::CreateFamilyDocument(BuiltInCategoryUniIdentities::BICU_POINT_FAMILY, strFamilyName);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空", L"GDMP", L"2023-09-30");
    std::vector<ElementId> modelViewIds = IModelView::GetAllModelViewIds(pDoc);

    OwnerPtr<IUserTransaction> opUt = IUserTransaction::Create(pDoc, L"创建点式族");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opUt, L"创建事务失败", L"GDMP", L"2023-09-30");
    InitFamilyDocument(pDoc);
    opUt->CommitAndResetHistory();

    IDocumentManager *pDocumentManager = IDocumentManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocumentManager, L"pDocumentManager为空", L"GDMP", L"2023-09-30");
    DocumentSaveOptions saveOptions(true);
    DocumentSaveResult saveResult;
    const std::wstring familyFileName = strFamilyName + pDocumentManager->GetFileExtensions()->GetDefaultFamilyFileExt();
    std::wstring strFile = FilePath::Combine(LogManager::Get()->GetLogsFolder(), familyFileName);

    bool bRet = pDoc->SaveAs(strFile, saveOptions, saveResult);

    pDocumentManager->CloseDocument(pDoc->GetRuntimeId());
    UiCommonDialog::ShowMessageBox(L"创建族文件", bRet ? L"创建成功, 保存路径：\n"+strFile : GBMP_TR(L"创建失败"), (int)UiCommonDialog::ButtonType::Yes);

    return nullptr;
}